using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Xml;
using mock_ehr_lib.Model.Infobutton;
namespace mock_ehr_lib.Model.Summary
{
    public class ImmunizationItem : IInfobuttonProvider
    {
        public string Vaccine { get; set; }
        public string Status { get; set; }
        public DateTime? StartDate { get; set; }
        public ImmunizationItem()
        {
            _infobuttonParameters = new List<InfobuttonParameter>
                                        {
                                            new InfobuttonParameter {Name = "info_context", Value = "OutPatientDrugs"},
                                            new InfobuttonParameter {Name = "info_institute", Value = "G4022"}
                                        };
        }
        public static IEnumerable<ImmunizationItem> FromCCD(XmlDocument ccdXml, XmlNamespaceManager namespaceManager)
        {
            var immunizationItems = new List<ImmunizationItem>();
            const string immunizationsXPath = "//ns:section[ns:templateId[@root='2.16.840.1.113883.10.20.1.6']]/ns:entry/ns:substanceAdministration";
            var immunizationNodes = ccdXml.SelectNodes(immunizationsXPath, namespaceManager);
            if (immunizationNodes != null)
            {
                var immunizationNodeEnumerator = immunizationNodes.GetEnumerator();
                while (immunizationNodeEnumerator != null && immunizationNodeEnumerator.MoveNext())
                {
                    var immunizationItem = new ImmunizationItem();
                    var current = immunizationNodeEnumerator.Current as XmlNode;
                    var effectiveTimeValueNode = current.SelectSingleNode("./ns:effectiveTime/ns:center/@value", namespaceManager);
                    if (effectiveTimeValueNode != null)
                    {
                        var effectiveTimeValue = effectiveTimeValueNode.Value;
                        var year = effectiveTimeValue.Substring(0, 4);
                        var month = effectiveTimeValue.Length >= 6 ? effectiveTimeValue.Substring(4, 2) : "1";
                        var day = effectiveTimeValue.Length >= 8 ? effectiveTimeValue.Substring(6, 2) : "1"; ;
                        immunizationItem.StartDate = DateTime.Parse(string.Format("{0}/{1}/{2}", month, day, year));
                    }
                    var substanceXPath = ".//ns:manufacturedProduct[ns:templateId[@root='2.16.840.1.113883.10.20.1.53']]//ns:manufacturedMaterial/ns:code";
                    var substanceNode = current.SelectSingleNode(substanceXPath, namespaceManager);
                    if (substanceNode != null)
                    {
                        var codeSystem = substanceNode.Attributes["codeSystem"].InnerText;
                        var code = substanceNode.Attributes["code"].InnerText;
                        // TODO create CVX lookup
                        //var lookupService = LookupServiceFactory.GetLookupServiceByCodeSystem(codeSystem);
                        //immunizationItem.Vaccine = lookupService.GetDescriptionByCode(code);
                        immunizationItem.Vaccine = substanceNode.Attributes["displayName"].InnerText;
                        immunizationItem._infobuttonParameters.Add(new InfobuttonParameter
                        {
                            Name = "info_med",
                            Value = immunizationItem.Vaccine
                        });
                    }
                    var statusXPath = ".//ns:statusCode/@code";
                    var statusNode = current.SelectSingleNode(statusXPath, namespaceManager);
                    if (statusNode != null)
                    {
                        immunizationItem.Status = statusNode.Value;
                    }
                    immunizationItems.Add(immunizationItem);
                }
            }
            return immunizationItems;
        }

        private List<InfobuttonParameter> _infobuttonParameters;
        public InfobuttonParameter[] GetParameters()
        {
            return _infobuttonParameters.ToArray();
        }
    }
}